Page({
  data: {
    bookList: [],
    imgUrl: "",
    keyword: "",
    historyArr: wx.getStorageSync("historyArr") || [],
  },
  // 1. 获取书籍列表
  getBookList() {
    wx.showLoading({
      title: "正在加载中...",
    });

    wx.request({
      url: "https://hmajax.itheima.net/api/books",
      data: {
        creator: "zhangsan",
      },
      method: "GET",
      success: (res) => {
        // console.log(result);
        setTimeout(() => {
          this.setData(
            {
              bookList: res.data.data,
            },
            () => {
              // console.log("该函数被执行, 意味着数据和视图都更新完毕了");
              wx.showToast({
                title: "查询书单成功!",
                icon: "none",
                mask: true,
              });
            }
          );
        }, 2000);
      },
      fail: (err) => {
        console.log(err);
      },
      complete: () => {
        setTimeout(() => {
          wx.hideLoading();
        }, 2000);
      },
    });
  },

  showModal() {
    wx.showModal({
      title: "温馨提示",
      content: "您确定退出登录吗?",
      complete: (res) => {
        console.log(res);
        if (res.cancel) {
        }

        if (res.confirm) {
        }
      },
    });
  },

  showActionSheet() {
    wx.showActionSheet({
      itemList: ["A", "B", "C", "D"],
    });
  },

  // 2. 数据的本地存储
  setLocalData() {
    const token = "2dwqdqwdwq212121";
    const person = {
      name: "张三",
      age: 18,
      sex: "男",
    };
    wx.setStorageSync("token", token);
    wx.setStorageSync("person", person);
  },
  getLocalData() {
    console.log(wx.getStorageSync("token"));
    console.log(wx.getStorageSync("person"));
  },
  delLocalData() {
    // wx.removeStorageSync("token");
    wx.removeStorageSync("person");
  },
  clearLocalData() {
    wx.clearStorageSync();
  },

  setLocalDataAsync() {
    wx.setStorage({
      key: "dog",
      data: { name: "旺财", age: 10, sex: "公" },
    });
  },
  getLocalDataAsync() {
    wx.getStorage({
      key: "dog",
      success: (res) => {
        console.log(res.data);
      },
    });
  },
  delLocalDataAsync() {
    wx.removeStorage({ key: "dog" });
  },
  clearLocalDataAsync() {
    wx.clearStorage();
  },

  // 3. 从本地选择文件
  async uploadImg() {
    // 3.1 写法1
    wx.chooseMedia({
      count: 1,
      mediaType: ["image"],
      success: (res) => {
        this.setData({
          imgUrl: res.tempFiles[0].tempFilePath,
        });
      },
      fail: (res) => {},
      complete: (res) => {},
    });

    // 3.2 写法2
    // wx.chooseMedia({
    //   count: 1,
    //   mediaType: ["image"],
    // }).then((res) => {
    //   this.setData({
    //     imgUrl: res.tempFiles[0].tempFilePath,
    //   });
    // });

    // 3.3 写法3
    // const res = await wx.chooseMedia({
    //   count: 1,
    //   mediaType: ["image"],
    // });
    // this.setData({
    //   imgUrl: res.tempFiles[0].tempFilePath,
    // });
  },

  // 4. 搜索
  doSearch() {
    // 4.1 取出内容
    if (!this.data.keyword.trim()) {
      wx.showToast({
        title: "搜索内容不能为空",
        icon: "none",
      });
      return;
    }
    // 4.2 把数据放入数组
    const tempArr = this.data.historyArr;
    tempArr.push(this.data.keyword);
    this.setData(
      {
        historyArr: tempArr,
        keyword: "",
      },
      () => {
        wx.setStorageSync("historyArr", this.data.historyArr);
      }
    );
  },

  clearHistory() {
    this.setData(
      {
        historyArr: [],
      },
      () => {
        wx.removeStorageSync("historyArr");
      }
    );
  },
});
